/*
   Copyright 2011 Jason Drake (jadrake75@gmail.com)
 
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
 
       http://www.apache.org/licenses/LICENSE-2.0
 
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package org.javad.components;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.text.MessageFormat;
import java.text.NumberFormat;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;

import org.bushe.swing.event.annotation.AnnotationProcessor;
import org.bushe.swing.event.annotation.EventSubscriber;
import org.javad.events.PositionEvent;
import org.javad.events.StatusEvent;
import org.javad.events.StatusEvent.StatusType;
import org.javad.events.ZoomEvent;

import pub.domain.GradientPanel;

@SuppressWarnings("serial")
public class StatusPanel extends GradientPanel {
	private JProgressBar progressBar;
	private JLabel lblStatusLabel;
	private JLabel lblZoomLabel;
	private JLabel lblPositionLabel;
	private MemoryStatus memoryStatus;
	private JPanel componentPanel;
	private JPanel progressPanel;
	
	public StatusPanel() {
		AnnotationProcessor.process(this);
		setForeground(UIHelper.Dark_Grey);
		setBackground(UIHelper.Medium_Grey);
		setPreferredSize(new Dimension(703, 25));
		setOpaque(false);
		setLayout(new BorderLayout(0, 0));
		add(getComponentPanel(), BorderLayout.EAST);
		add(getProgressPanel(), BorderLayout.WEST);
	}
	
	public void showZoomLabel(boolean showZoom) {
		getLblZoomLabel().setVisible(showZoom);
	}
	
	public void showPositionLabel(boolean showPosition) {
		getLblPositionLabel().setVisible(showPosition);
	}
	
	@EventSubscriber(eventClass=PositionEvent.class) 
	public void onPositionChange(PositionEvent event) {
		getLblPositionLabel().setText( event.getPoint().x + "," + event.getPoint().y);
	}
	@EventSubscriber(eventClass=ZoomEvent.class)
	public void onZoom(ZoomEvent event) {
		NumberFormat formatter = NumberFormat.getPercentInstance();
		getLblZoomLabel().setText(MessageFormat.format(ComponentResources.getString("label.zoom.ratio"), formatter.format(event.getRatio())));
	}
	

	@EventSubscriber(eventClass=StatusEvent.class)
	public void onStatusUpdate(StatusEvent event) {
		if( event.getType() == StatusType.ShowBusy) {
			getProgressBar().setIndeterminate(true);
			getProgressBar().setVisible(true);
		}
		switch( event.getType()) {
		case ShowBusy: case Message:
			String text = event.getData();
			if( text != null ) {
				getLblStatusLabel().setText(text);
				getLblStatusLabel().setVisible(true);
			} else {
				getLblStatusLabel().setVisible(false);
			}
			break;
		case Progress:
			int value = Integer.parseInt(event.getData().toString());
			if( value > 0 ) {
				getProgressBar().setIndeterminate(false);
				getProgressBar().setValue(value);
				getProgressBar().setVisible(true);
			} else {
				getProgressBar().setVisible(false);
			}
			break;
		case Finished:
			getProgressBar().setVisible(false);
			text = event.getData();
			if( text != null ) {
				getLblStatusLabel().setText(text);
				getLblStatusLabel().setVisible(true);
			} else {
				getLblStatusLabel().setVisible(false);
			}
			break;
		case MaximumProgress:
			Integer total = event.getData();
			getProgressBar().setMaximum(total);
		}
		
	}
	
	protected JProgressBar getProgressBar() {
		if (progressBar == null) {
			progressBar = new JProgressBar();
			progressBar.setPreferredSize(new Dimension(120, 17));
			progressBar.setName("status-progressBar");
			progressBar.setVisible(false);
		}
		return progressBar;
	}
	protected JLabel getLblStatusLabel() {
		if (lblStatusLabel == null) {
			lblStatusLabel = new JLabel("");
			lblStatusLabel.setPreferredSize(new Dimension(250, 17));
			lblStatusLabel.setForeground(new Color(255, 255, 255));
		}
		return lblStatusLabel;
	}
	protected JLabel getLblZoomLabel() {
		if (lblZoomLabel == null) {
			lblZoomLabel = new JLabel(ComponentResources.getString("label.zoom.full"));
			lblZoomLabel.setHorizontalAlignment(SwingConstants.CENTER);
			lblZoomLabel.setPreferredSize(new Dimension(100, 15));
			lblZoomLabel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		}
		return lblZoomLabel;
	}
	protected JLabel getLblPositionLabel() {
		if (lblPositionLabel == null) {
			lblPositionLabel = new JLabel(ComponentResources.getString("label.coord.init"));
			lblPositionLabel.setHorizontalAlignment(SwingConstants.CENTER);
			lblPositionLabel.setPreferredSize(new Dimension(100, 15));
			lblPositionLabel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		}
		return lblPositionLabel;
	}
	protected MemoryStatus getMemoryStatus() {
		if (memoryStatus == null) {
			memoryStatus = new MemoryStatus();
			memoryStatus.setPreferredSize(new Dimension(75, 17));
			memoryStatus.setBorder(null);
		}
		return memoryStatus;
	}
	protected JPanel getComponentPanel() {
		if (componentPanel == null) {
			componentPanel = new JPanel();
			componentPanel.setOpaque(false);
			FlowLayout fl_componentPanel = (FlowLayout) componentPanel.getLayout();
			fl_componentPanel.setAlignment(FlowLayout.RIGHT);
			componentPanel.add(getProgressBar());
			componentPanel.add(getMemoryStatus());
			componentPanel.add(getLblPositionLabel());
			componentPanel.add(getLblZoomLabel());
		}
		return componentPanel;
	}
	protected JPanel getProgressPanel() {
		if (progressPanel == null) {
			progressPanel = new JPanel();
			progressPanel.setOpaque(false);
			progressPanel.add(getLblStatusLabel());
		}
		return progressPanel;
	}
}
